//
//  FSWSegumentionView.m
//  quhuolang_driver
//
//  Created by 曹欣欣 on 2018/3/13.
//  Copyright © 2018年 fusw. All rights reserved.
//

#import "FSWSegumentionView.h"
#import "FSWSegmentedControl.h"

@interface FSWSegumentionView ()<UIScrollViewDelegate>

@property (nonatomic,strong) FSWSegmentedControl* segmentionView;

@property (nonatomic,strong) UIScrollView* scrollView;

@property (nonatomic,strong) NSMutableArray<UIView*>* controllerViews;

@property (nonatomic,assign) BOOL isButtonAction;

@property (nonatomic,assign) NSInteger index;

@property (nonatomic,strong) NSArray* titles;

@property (nonatomic,copy) clickBlock block;

@property (nonatomic, assign) CGFloat itemWidth;

@end

@implementation FSWSegumentionView

- (instancetype)initWithFrame:(CGRect)frame viewControllers:(NSArray<UIViewController *> *)viewControllers titles:(NSArray *)titles itemWidth:(CGFloat)itemWidth andBlock:(clickBlock)block
{
    @weakify(self);
    if (self = [super initWithFrame:frame]) {
        self.index = 0;
        self.titles = titles;
        self.block = block;
        self.itemWidth = itemWidth;
        
//        [self.segmentionView removeFromSuperview];
        self.segmentionView = [FSWSegmentedControl headView];
        self.segmentionView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 75);
        [self.segmentionView showTitles:titles andItemWidth:itemWidth andItemHeight:75 backblock:^(NSInteger index) {
            @strongify(self);
            if ([self.titles[index] isEqual:@""]) {
                return ;
            }
            self.isButtonAction = YES;
            [UIView animateWithDuration:0.5 animations:^{
                @strongify(self);
                self.scrollView.contentOffset = CGPointMake(index * CGRectGetWidth(self.frame), 0);
            } completion:nil];
//            if (self.block) {
//                self.block(self.scrollView.contentOffset.x / SCREEN_WIDTH);
//            }
        }];
        
//        self.segmentionView = [[FSWSegmentedControl alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 75) titles:titles andItemWidth:itemWidth backblock:^(NSInteger index) {
//            @strongify(self);
//            if ([self.titles[index] isEqual:@""]) {
//                return ;
//            }
//            self.isButtonAction = YES;
//            [UIView animateWithDuration:0.5 animations:^{
//                @strongify(self);
//                self.scrollView.contentOffset = CGPointMake(index * CGRectGetWidth(self.frame), 0);
//            } completion:nil];
//            if (self.block) {
//                self.block(self.scrollView.contentOffset.x / SCREEN_WIDTH);
//            }
//        }];
        [self addSubview:self.segmentionView];
        
        [self initControllerView:viewControllers];
        [self addControllerView];
        
        self.scrollView.delegate = self;
        if ([self.titles.lastObject isEqual:@""]) {
            self.scrollView.contentSize = CGSizeMake((self.titles.count - 1) * CGRectGetWidth(self.frame), CGRectGetHeight(self.frame) - 94);
        }else{
            self.scrollView.contentSize = CGSizeMake(self.titles.count * CGRectGetWidth(self.frame), CGRectGetHeight(self.frame) - 94);
        }
        self.scrollView.pagingEnabled = YES;
    }
    return self;
}

#pragma mark --Delegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.isButtonAction = NO;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x < 0 || scrollView.contentOffset.x / (self.titles.count - 1 - ([self.titles.lastObject isEqual:@""] ? 1: 0)) > CGRectGetWidth(self.frame)) return;
    
    [self.segmentionView moveLine:scrollView.contentOffset.x / (SCREEN_WIDTH / self.itemWidth)];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    XCLog(@"%@",self.titles[(NSInteger)(scrollView.contentOffset.x / (SCREEN_WIDTH  / self.itemWidth) / self.itemWidth)]);
    if ([self.titles[(NSInteger)(scrollView.contentOffset.x / (SCREEN_WIDTH  / self.itemWidth) / self.itemWidth)] isEqual:@""]) {
        return;
    }
    if (self.block) {
        self.block(scrollView.contentOffset.x / SCREEN_WIDTH);
    }
}

#pragma mark - Public Method
- (void)scrollToPage:(NSInteger)pageIndex
{
    self.scrollView.contentOffset = CGPointMake(SCREEN_WIDTH * pageIndex, 0);
    
}


- (void)orderNumberData:(NSArray *)data{
    [self.segmentionView orderNumberData:data];
}
#pragma mark --Private Method
- (void)addControllerView
{
    [self addSubview:self.scrollView];
    __weak typeof(self) weakself = self;
    [self.controllerViews enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.frame = CGRectMake(CGRectGetWidth(weakself.frame) * idx, 0, CGRectGetWidth(weakself.frame), weakself.scrollView.xc_height);
        [weakself.scrollView addSubview:obj];
    }];
}

- (void)initControllerView:(NSArray*)viewControllers
{
    [self.controllerViews removeAllObjects];
    for (UIViewController* viewController in viewControllers) {
        [[self belongViewController] addChildViewController:viewController];
        [self.controllerViews addObject:viewController.view];
    }
}

#pragma mark --Get/Set
- (NSMutableArray<UIView *> *)controllerViews
{
    if (!_controllerViews) {
        _controllerViews = [NSMutableArray array];
    }
    return _controllerViews;
}

- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 75, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame) - 75)];
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.backgroundColor = [UIColor colorWithHexString:@"#F6F7FB"];
    }
    return _scrollView;
}

@end

